package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Category;
import com.online.onlinestudent.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categorys")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public AjaxResult getAllCategories() {
        List<Category> categories = categoryService.list();
        if (categories == null || categories.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(categories);
    }

    @GetMapping("/{id}")
    public AjaxResult getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        if (category != null) {
            return AjaxResult.success(category);
        } else {
            return AjaxResult.fail("不存在该分类！");
        }
    }

    @PostMapping
    public AjaxResult saveCategory(@RequestBody Category category) {
        try {
            boolean saved = categoryService.save(category);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败，已存在该分类！");
        }
    }

    @PutMapping("/{id}")
    public AjaxResult updateCategory(@PathVariable Long id, @RequestBody Category category) {
        category.setId(id);
        try {
            boolean updated = categoryService.updateById(category);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteCategoryById(@PathVariable Long id) {
        try {
            boolean deleted = categoryService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该分类！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getCategoriesByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Category> page = categoryService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}
